﻿
using System;
using Object = UnityEngine.Object;
namespace FrameWorkSong
{
    public enum ResState
    {
        Waiting,
        Loading,
        Loaded
    }

    public abstract class Res : SimpleRefCount
    {
        public ResState State
        {
            get { return my_State; }
            protected set
            {
                my_State = value;
                //模式改变触发
                if (my_State==ResState.Loaded)
                {
                    if (my_OnLoadedEvent!=null)
                    {
                        my_OnLoadedEvent.Invoke(this);
                    } 
                }
            }
        }
        public Object Asset { get; protected set; }
        public string Name { get; protected set; }
        // public string Name { get { return Asset.name; } }
        public abstract bool LoadAssetSync();
        public abstract void LoadAssetAsync();
        protected abstract void OnReleaseRes();
        private ResState my_State;
        /// <summary>
        /// 归零时清除
        /// </summary>
        protected override void OnZeroRef()
        {
            OnReleaseRes();

        }
        /// <summary>
        /// 销毁方法
        /// </summary>
        public abstract void ReleaseAsset();

        private event Action<Res> my_OnLoadedEvent;

        public void RegisterOnLoadedEvent(Action<Res> onLoaded)
        {
            my_OnLoadedEvent += onLoaded;
            
        }
        public void UnRegisterOnLoadedEvent(Action<Res> onLoaded)
        {
            my_OnLoadedEvent -= onLoaded;
        }


    }
}
